//
//  YZELookforPwdTableViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/4/1.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "YZELookforPwdTableViewController.h"
#import <QMUIKit/QMUIKit.h>
#import "CommonDefine.h"
#import "SelfTools.h"
#import "MD5.h"

#define URL_ResetPWD @"/api/account/reset_phone"

@interface YZELookforPwdTableViewController ()<NetworkUtilsDelegate>
@property (strong, nonatomic) IBOutlet QMUITextField *m_textfield_mobi;
@property (strong, nonatomic) IBOutlet QMUITextField *m_textfield_pwd;
@property (strong, nonatomic) IBOutlet QMUITextField *m_textfield_captcha;
@property (strong, nonatomic) IBOutlet UIButton *btn_captcha;
@property (nonatomic, strong) QMUIFillButton *m_btn_resetpwd;
@end

@implementation YZELookforPwdTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"找回密码";
    self.tableView.allowsSelection = YES;
    ;
    
    [self.btn_captcha addTarget:self action:@selector(onGetvcClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

//footer重置密码按钮
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return FOOTER_HEIGHT;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *rowview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FOOTER_HEIGHT)];
    _m_btn_resetpwd = [[QMUIFillButton alloc] initWithFillType:QMUIFillButtonColorBlue];
    [self.m_btn_resetpwd setTitle:@"重置密码" forState:UIControlStateNormal];
    [self.m_btn_resetpwd setFrame:CGRectMake((SCREEN_WIDTH-BTN_ROW_WIDTH)/2, (FOOTER_HEIGHT-BTN_ROW_HEIGHT)/2, BTN_ROW_WIDTH, BTN_ROW_HEIGHT)];
    [self.m_btn_resetpwd addTarget:self action:@selector(reqResetPwd) forControlEvents:UIControlEventTouchUpInside];
    [rowview addSubview:self.m_btn_resetpwd];
    if (section!=0) {
        return nil;
    }
    return rowview;
}

#pragma mark - 网络代理
- (void)OnSuccess:(id)responseObject requestTag:(int)tag{}
- (void)OnFail:(NSString *)errorMsg errorCode:(NSInteger)errorCode requestTag:(int)tag{
    [QMUITips hideAllToastInView:self.view animated:YES];
    if (errorCode==3) {
        [UIView showToastWindowWithMessage:@"医生账号只能使用医生版客户端App!"];
    }
    else {
        [UIView showToastWindowWithMessage:errorMsg];
    }
}

#pragma mark - UI响应
//获取验证码点击响应
-(void)onGetvcClick:(UIButton *) sender{
    if ([SelfTools checkMobile:self.m_textfield_mobi.text]) {
        YZEModel *request=[YZEModel sharedInstace];
        [request requestGetCaptcha:self.m_textfield_mobi.text delegate:nil requestTag:1];
        [self receiveCheckNumButton];
    }
    else{
        [QMUITips showError:@"请输入正确的手机格式" inView:self.view hideAfterDelay:2];
    }
    
    
}
//获取验证码倒计时
- (void)receiveCheckNumButton{
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.btn_captcha setTitle:@"获取验证码" forState:UIControlStateNormal];
                self.btn_captcha.userInteractionEnabled = YES;
                
            });
        }else{
            int seconds = timeout;
            NSString *strTime = [NSString stringWithFormat:@"%d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.btn_captcha.userInteractionEnabled = NO;
                //设置界面的按钮显示 根据自己需求设置
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [self.btn_captcha setTitle:[NSString stringWithFormat:@"重发(%@s)",strTime] forState:UIControlStateNormal];
                [UIView commitAnimations];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

#pragma mark - 请求重置密码
- (void)reqResetPwd{
    NSString *mobi = self.m_textfield_mobi.text;
    NSString *pwd = self.m_textfield_pwd.text;
    NSString *captcha = self.m_textfield_captcha.text;
    
    if (!mobi || ![mobi isNotEmpty]) {
        [UIView showToastWindowWithMessage:@"请输入手机号码"];
        return;
    }
    if (!pwd || ![pwd isNotEmpty]) {
        [UIView showToastWindowWithMessage:@"请输入新密码"];
        return;
    }
    if (!captcha || ![captcha isNotEmpty]) {
        [UIView showToastWindowWithMessage:@"请输入验证码"];
        return;
    }
    
    pwd = [MD5 getMd5_32Bit_String:pwd];
    
    NSDictionary *formdata = [NSDictionary dictionaryWithObjectsAndKeys:pwd, @"password", mobi, @"phone", captcha, @"verifycode", nil];
    [[YZENetwork sharedInstace]postRequestWithUrl:[BASE stringByAppendingString:URL_ResetPWD] parameters:formdata delegate:self requestTag:2 successBlock:^(id responseObject) {
        [QMUITips hideAllToastInView:self.view animated:YES];
        [QMUITips showSucceed:@"密码已重置" inView:self.view hideAfterDelay:2];
    }];
    [QMUITips showLoadingInView:self.view];
}

@end
